package com.daxiong.hfs.util;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;

import com.daxiong.hfs.bean.MainBean;
import com.daxiong.hfs.bean.ProductBean;

public class CommonUtil {

	private static String getJSON(final String url) {
		String json = StringUtils.EMPTY;

		try {
			final HttpParams httpParams = new BasicHttpParams();

			HttpConnectionParams.setConnectionTimeout(httpParams, 30000);

			final HttpClient httpclient = new DefaultHttpClient(httpParams);

			final HttpGet httpGet = new HttpGet(url);

			final HttpResponse response = httpclient.execute(httpGet);

			if (response.getStatusLine().getStatusCode() == 200) {
				json = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
			}
		} catch (ClientProtocolException e) {
		} catch (ParseException e) {
		} catch (IOException e) {
		}

		return json;
	}

	public static List<ProductBean> getProductBeanList(final String url) {
		return processJSON(getJSON(url));
	}

	public static Serializable getSerializable( //
			final Intent intent, //
			final String key //
	) {
		final Bundle bundle = intent.getExtras();

		return bundle.getSerializable(key);
	}

	private static boolean isApproves(final String status) {
		if ("核可".contains(status)) {
			return true;
		} else {
			return false;
		}
	}

	private static List<ProductBean> processJSON(final String json) {
		final List<ProductBean> productList = new ArrayList<ProductBean>();

		try {
			final JSONArray jsonArray = new JSONArray(json);

			for (int i = 0; i < jsonArray.length(); i++) {
				final JSONArray info = jsonArray.getJSONArray(i);

				final String qualifiedId = //
				processString(info.getJSONObject(0).getString("許可證字號"));
				final String category = //
				processString(info.getJSONObject(1).getString("類別"));
				final String chineseName = //
				processString(info.getJSONObject(2).getString("中文品名"));
				final String date = //
				processString(info.getJSONObject(3).getString("核可日期"));
				final String company = //
				processString(info.getJSONObject(4).getString("申請商"));
				final String status = //
				processString(info.getJSONObject(5).getString("證況"));
				final String content = //
				processString(info.getJSONObject(6).getString("保健功效相關成分"));
				final String effect = //
				processString(info.getJSONObject(7).getString("保健功效"));
				final String declare = //
				processString(info.getJSONObject(8).getString("保健功效宣稱"));
				final String warnings = //
				processString(info.getJSONObject(9).getString("警語"));
				final String precautions = //
				processString(info.getJSONObject(10).getString("注意事項"));

				if (!isApproves(status)) {
					continue;
				}

				final ProductBean product = new ProductBean();

				product.setQualifiedId(qualifiedId);
				product.setCategory(category);
				product.setChineseName(chineseName);
				product.setDate(date);
				product.setCompany(company);
				product.setStatus(status);
				product.setContent(content);
				product.setEffect(effect);
				product.setDeclare(declare);
				product.setWarnings(warnings);
				product.setPrecautions(precautions);

				productList.add(product);
			}
		} catch (JSONException e) {
		}

		return productList;
	}

	public static void processResultList(final MainBean mainBean) {
		final List<ProductBean> resultList = new ArrayList<ProductBean>();

		for (final ProductBean productBean : mainBean.getProductBeanList()) {
			final ProductBean queryConditions = mainBean.getQueryConditions();
			final String queryChineseName = queryConditions.getChineseName();
			final String queryQualifiedId = queryConditions.getQualifiedId();
			final String queryCompany = queryConditions.getCompany();

			final String chineseName = productBean.getChineseName();
			final String qualifiedId = productBean.getQualifiedId();
			final String company = productBean.getCompany();

			if (StringUtils.isNoneBlank(queryChineseName)) {
				if (!StringUtils.contains(chineseName, queryChineseName)) {
					continue;
				}
			}

			if (StringUtils.isNoneBlank(queryQualifiedId)) {
				if (!StringUtils.contains(qualifiedId, queryQualifiedId)) {
					continue;
				}
			}

			if (StringUtils.isNoneBlank(queryCompany)) {
				if (!StringUtils.contains(company, queryCompany)) {
					continue;
				}
			}

			resultList.add(productBean);
		}

		mainBean.setResultList(resultList);
	}

	private static String processString(final String string) {
		return StringUtils.isNotBlank(string) ? string : "無";
	}

	public static void putSerializable( //
			final Intent intent, //
			final Serializable object, //
			final String key //
	) {
		final Bundle bundle = new Bundle();

		bundle.putSerializable(key, object);

		intent.putExtras(bundle);
	}

}
